var config = require('../../../utils/config.js');
var util = require('../../../utils/util.js');
var API_URL = config.API_URL;
var app = getApp();
let loading, page, id;
let getData = (that) => {
  if (loading) {
    return
  }
  loading = true;
  util.PRL(API_URL.goods_comment_info, { comment_id: id, page: page })
    .then(res => {
      console.log(res);
      let comment = res.data.data.head;
      comment.id = id;
      let allCount = res.data.data.foot.allCount;
      let list;
      if (page == 1) {
        list = res.data.data.foot.commentInfo;
      } else {
        list = that.data.list.concat(res.data.data.foot.commentInfo);
      }
      that.setData({
        comment: comment,
        allCount: allCount,
        list: list,
        hidden: true
      })
      if (res.data.data.foot.commentInfo.length != 0 || res.data.hasMore != 0) {
        page++;
      }
      loading = false;
    }).catch(res => {
      loading = false;
      console.log(res);
    })
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //   获取该评论的ID 
    id = options.id;
    page = 1;
    loading = false;
    getData(this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    getData(this)
  },
  like: function (e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    // 把点赞的商品id 发送给后台处理
    util.PRL(API_URL.goods_comment_like, { comment_id: id })
      .then(res => {
        if (res.data.status == 300) {
          my.alert({
            title: '提示',
            content: res.data.msg,
            buttonText: '确定'
          });
        } else {
          my.showToast({
            content: res.data.msg,
            type: 'success',
            duration: 800
          })
          let comment = that.data.comment;
          let like = Number(comment.like);
          comment.like = like + 1;
          that.setData({
            comment: comment
          })
        }
      }).catch(res => {
        console.log(res);
      })
  },
  comment_btn: function (e) {
    this.setData({
      hidden: false
    })
  },
  hidden: function (e) {
    this.setData({
      hidden: true
    })
  },
  comment: function (e) {
    let that = this;
    // 获取评论的内容
    let comment = e.detail.value.comment;
    console.log(e)
    util.PRL(API_URL.goods_comment_reply, { comment_id: id, comment: comment })
      .then(res => {
        console.log(res)
        my.showToast({
          content: '操作成功',
          duration: 800
        })
        page = 1;
        loading = false;

        getData(that);
      }).catch(res => {
        console.log(res);
      })
  }
})